﻿import mx.utils.Delegate;
import mx.events.EventDispatcher; 
class A2.XmlLogFile
{
	private var vars:Object;
	public var oconfig:Object;
	public var addEventListener:Function;
	private var dispatchEvent:Function;
	public var removeEventListener:Function;
	public static var _A_:Object = new Object ();
	public static var _x_:Object = new Object ();
	public static var isLoaded:Boolean = false;
	public function XmlLogFile(file:String,vars:Object)
	{
		var xml:XML = new XML();
		xml.ignoreWhite = true;
		var t = this;
		this.vars = vars
		/*var lis:Object = new Object();
		lis.ok = function(_oconfig:Object):Object
		{
			_x_=_oconfig;
			trace(_x_.item[0].title)
			return(_x_);
		};*/
		
		EventDispatcher.initialize(this);
		//addEventListener("my_event", this); 
		/*AsBroadcaster.initialize(_A_);
		_A_.addListener(lis);*/
		//_root['mpo'].trace(' 初始类 A2.XmlLogFile.as ' ); 
		xml.onLoad = Delegate.create(this, function (success)
		{
			//_root['mpo'].trace(' XML下载  onLoad success = '+ success  ); 
			if (success)
			{
				isLoaded = true;
				oconfig = getChildAsObject(xml.childNodes[0]);
				//_A_.broadcastMessage("ok",oconfig);
				//类内部直接在这调用你想执行的函数就行了.
				//这个事件是发送给类外部的接收者的
				trace('this.vars.id - '+this.vars.id)
				dispatchEvent({type:"my_event", target:oconfig,a:1,vars:this.vars}); 
			} else
			{
				//_root['mpo'].trace(' XML下载失败，调用onNO '  ); 
				 trace(' XML下载失败，调用onNO '  ); 
				dispatchEvent({type:"onNO"});  
			}
		});
		xml.load(file);
	}
	/*private function my_event(){
		trace(oconfig.item[0].title)
	}*/
	private function getChildAsObject(xObj)
	{
		var obj = new Object();
		var a:Object, c:Object, nName:String, nType:Number, nValue:String, cCount:Number;
		for (a in xObj.attributes)
		{
			obj[a] = xObj.attributes[a];
		}
		for (c in xObj.childNodes)
		{
			nName = xObj.childNodes[c].nodeName;
			nType = xObj.childNodes[c].nodeType;
			nValue = xObj.childNodes[c].nodeValue;
			if (nType == 3)
			{
				obj._value = nValue;
				obj._type = 'text';
			}
			if (nType == 1 && nName != null)
			{
				if (obj[nName] == null)
				{
					obj[nName] = this.getChildAsObject(xObj.childNodes[c]);
				} else if (obj[nName]._type != 'array')
				{
					obj[nName] = [obj[nName]];
					obj[nName]._type = 'array';
				}
				if (obj[nName]._type == 'array')
				{
					obj[nName].unshift(this.getChildAsObject(xObj.childNodes[c]));
				}
			}
		}
		return obj;

	}
	function showerror(errstr:String)
	{
		trace("r");
	}
}